/*
 * Copyright (c) YouYuan 2018. https://gitee.com/you-yuan
 */

package com.yuan.crawler.company.core;

import com.gargoylesoftware.htmlunit.ProxyConfig;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.yuan.crawler.common.HtmlUnitCommon;
import com.yuan.crawler.company.bean.CompanyInfo;
import com.yuan.crawler.proxy.IPProxy;
import com.yuan.crawler.proxy.ProxyServer;
import com.yuan.crawler.proxy.impl.ZMProxyServerImpl;
import com.yuan.util.collection.ListUtil;

import java.util.List;

/**
 * 企业信息爬取
 *
 * @author YouYuan
 * @version 1.0
 * @date 2018/9/6 09:45
 */
public class CompanyInfoCrawler {

    private static final String TYC_API = "https://www.tianyancha.com/search?key=";

    private static final ProxyServer PROXY_SERVER = new ZMProxyServerImpl();

    public static boolean OPEN_PROXY = true;

    public static void main(String[] args) throws Exception {
        CompanyInfo companyInfo = searchCompanyInfo("腾讯");
        System.out.println(companyInfo);
    }

    public static CompanyInfo searchCompanyInfo(String companyName) throws Exception {
        String searchUrl = TYC_API + companyName;
        WebClient webClient = HtmlUnitCommon.initWebClient();
        if (OPEN_PROXY) {
            //设置代理
            List<IPProxy> proxyList = PROXY_SERVER.requestNewProxy();
            if (ListUtil.notEmpty(proxyList)) {
                IPProxy proxy = proxyList.get(0);
                webClient.getOptions().setProxyConfig(new ProxyConfig(proxy.getIp(), proxy.getPort()));
                System.out.println("[Proxy IP]" + proxy.getIpAndPort());
            }
        }
        HtmlPage page = webClient.getPage(searchUrl);
//        System.out.println(page.asXml());
        List<HtmlAnchor> resultList = page.getByXPath("//a[@tyc-event-ch='CompanySearch.Company']");
        if (ListUtil.notEmpty(resultList)) {
            String companyHref = resultList.get(0).getHrefAttribute();//获取默认第一个最匹配的公司链接
            HtmlPage companyPage = webClient.getPage(companyHref);
            CompanyInfo companyInfo = new CompanyInfo();
//            System.out.println(companyPage.asXml());
            //获取公司名称
            List<HtmlElement> companyNameList = companyPage.getByXPath("//h1[@class='name']");
            if (ListUtil.notEmpty(companyNameList)) {
                String name = companyNameList.get(0).getTextContent();
                companyInfo.setName(name);
            }
            //获取公司网址
            List<HtmlElement> companyLinkList = companyPage.getByXPath("//a[@class='company-link']");
            if (ListUtil.notEmpty(companyLinkList)) {
                String companyLink = companyLinkList.get(0).getAttribute("href");
                companyInfo.setCompanyLink(companyLink);
            }
            //获取公司地址
            List<HtmlElement> addressList = companyPage.getByXPath("//span[@class='address']");
            if (ListUtil.notEmpty(addressList)) {
                String address = addressList.get(0).getAttribute("title");
                companyInfo.setAddress(address);
            }
            //获取公司信息
            List<HtmlElement> dataContentList = companyPage.getByXPath("//div[@id='_container_baseInfo']");
            if (ListUtil.notEmpty(dataContentList)) {
                //获取公司法人
                HtmlElement dataContent = dataContentList.get(0);
                List<HtmlElement> farenList = dataContent.getByXPath("//div[@class='humancompany']/div[@class='name']/a");
                if (ListUtil.notEmpty(farenList)) {
                    String farenName = farenList.get(0).getTextContent();
                    companyInfo.setJuridicalPerson(farenName);
                }
                //获取注册资本
                List<HtmlElement> zbList = dataContent.getByXPath("//tbody/tr/td[2]/div[2]");
                if (ListUtil.notEmpty(zbList)) {
                    String registeredCapital = zbList.get(0).getAttribute("title");
                    companyInfo.setRegisteredCapital(registeredCapital);
                }
                //获取公司状态
                List<HtmlElement> stateList = dataContent.getByXPath("//tbody/tr[3]/td/div[2]");
                if (ListUtil.notEmpty(stateList)) {
                    String state = stateList.get(0).getAttribute("title");
                    companyInfo.setState(state);
                }
                //获取工商注册号
                List<HtmlElement> gsNoList = companyPage.getByXPath("//table[@class='table -striped-col -border-top-none']/tbody[1]/tr[1]/td[2]");
                if (ListUtil.notEmpty(gsNoList)) {
                    String gsNo = gsNoList.get(0).getTextContent();
                    companyInfo.setBusinessRegistrationNumber(gsNo);
                }
                //获取组织机构代码
                List<HtmlElement> zzjgList = companyPage.getByXPath("//table[@class='table -striped-col -border-top-none']/tbody[1]/tr[1]/td[4]");
                if (ListUtil.notEmpty(zzjgList)) {
                    String organizationCode = zzjgList.get(0).getTextContent();
                    companyInfo.setOrganizationCode(organizationCode);
                }
                //获取统一信用代码
                List<HtmlElement> tyxyList = companyPage.getByXPath("//table[@class='table -striped-col -border-top-none']/tbody[1]/tr[2]/td[2]");
                if (ListUtil.notEmpty(tyxyList)) {
                    String unifiedCreditCode = tyxyList.get(0).getTextContent();
                    companyInfo.setUnifiedCreditCode(unifiedCreditCode);
                }
                //获取公司类型
                List<HtmlElement> typeList = companyPage.getByXPath("//table[@class='table -striped-col -border-top-none']/tbody[1]/tr[2]/td[4]");
                if (ListUtil.notEmpty(typeList)) {
                    String type = typeList.get(0).getTextContent();
                    companyInfo.setType(type);
                }
                //获取纳税人识别号
                List<HtmlElement> tinList = companyPage.getByXPath("//table[@class='table -striped-col -border-top-none']/tbody[1]/tr[3]/td[2]");
                if (ListUtil.notEmpty(tinList)) {
                    String tin = tinList.get(0).getTextContent();
                    companyInfo.setTIN(tin);
                }
                //获取行业
                List<HtmlElement> tradeList = companyPage.getByXPath("//table[@class='table -striped-col -border-top-none']/tbody[1]/tr[3]/td[4]");
                if (ListUtil.notEmpty(tradeList)) {
                    String trade = tradeList.get(0).getTextContent();
                    companyInfo.setTrade(trade);
                }
                //获取营业期限
                List<HtmlElement> yyqxList = companyPage.getByXPath("//table[@class='table -striped-col -border-top-none']/tbody[1]/tr[4]/td[2]");
                if (ListUtil.notEmpty(yyqxList)) {
                    String timeLimit = yyqxList.get(0).getTextContent();
                    companyInfo.setTimeLimit(timeLimit);
                }
                //获取核准日期
                List<HtmlElement> approvalDateList = companyPage.getByXPath("//table[@class='table -striped-col -border-top-none']/tbody[1]/tr[4]/td[4]");
                if (ListUtil.notEmpty(approvalDateList)) {
                    String approvalDate = approvalDateList.get(0).getTextContent();
                    //TODO 时间格式被混淆，数据不可直接用
                    //companyInfo.setApprovalDate(approvalDate);
                }
                //获取经营范围
                List<HtmlElement> scopeOfBusinessList = companyPage.getByXPath("//table[@class='table -striped-col -border-top-none']/tbody[1]/tr[4]/td[4]");
                if (ListUtil.notEmpty(scopeOfBusinessList)) {
                    //TODO 经营范围被混淆，数据不可直接用
                }
                return companyInfo;
            }
        }
        return null;
    }

}
